$(function() {

    layui.config({
        base: '/Public/'
    }).extend({
        formSelects: 'layui-formSelects/src/formSelects-v4'
    });

    layui.use(['element','layer','form','upload','formSelects'],function(){
        var element = layui.element,
            form = layui.form,
            upload = layui.upload,
            formSelects = layui.formSelects;

        /**
         * 工作流程
         */
        var anchor;
        var _layid;

        //获取hash来切换选项卡，假设当前地址的hash为lay-id对应的值
        var layid = location.hash.replace(/^#tab=/, '');
        element.tabChange('tab', layid);

        //监听Tab切换，以改变地址hash值
        element.on('tab(tab)', function(){
            //获取当前点击的tab
            _layid = this.getAttribute('lay-id');
            //判断是否点击了步骤
            if (anchor !== undefined && _layid === 'progress' && anchor !== '') {
                location.hash = 'tab='+ _layid + '&anchor='+anchor;
            } else {
                location.hash = 'tab='+ _layid;
                anchor = '';//清空anchor
            }
        });

        //点击步骤改变url
        $(document).on('click', '.process-title a', function (e) {
            //阻止默认事件
            e.preventDefault();
            //获取当前点击步骤
            var step = $(this).attr('data-id');
            location.hash = 'tab='+ _layid + '&anchor=' + step;
        });

        //定位到指定位置
        function getAnchor() {
            var hash = location.hash;
            var data = [];

            //判断是否有&存在
            if (hash.indexOf("&") !== -1) {

                //拆分hash
                hash = hash.split('#');
                hash = hash[1].split('&');
                for (var i = 0; i<hash.length; i++) {
                    data[i] = hash[i].split('=');
                }

                //获取到tab的值
                _layid = data[0][1];

                //获取到anchor的值
                anchor = data[1][1];

                //切换到指定tab
                element.tabChange('tab', _layid);

                //定位到该步骤
                $('html,body').animate({scrollTop: $('#'+ anchor ).offset().top-41}, 300);
            }
        }

        //页面加载时
        getAnchor();

        //URL中的hash值变化时
        window.onhashchange = getAnchor;


        /**
         * 点击操作按钮弹出操作框
         */
        $("#operate").click(function () {
            layer.open({
                id: 'handle-dialog',
                type: 1,
                title: '添加处理记录',
                content: $('.operate'),
                area: ['650px'],
                //offset: '100px',
                shadeClose: true,
            })
        });

        //监听类型选择
        form.on('select(operation)',function (data) {
            var _value = data.value;    //获取到当前选中value

            //根据当前操作类型显示对应的处理人列表
            if (_value === 'push' || _value === 'transfer' || _value === 'rollback') {
                if (_value === 'push') {
                    $("#operator").css({ display:'block' });
                    $("#transfer").css({ display:'none' });
                    $("#rollback").css({ display:'none' });
                }
                if (_value === 'transfer') {
                    $("#operator").css({ display:'none' });
                    $("#transfer").css({ display:'block' });
                    $("#rollback").css({ display:'none' });
                }
                if (_value === 'rollback') {
                    $("#operator").css({ display:'none' });
                    $("#transfer").css({ display:'none' });
                    $("#rollback").css({ display:'block' });
                }
            } else {
                $("#operator").css({ display:'none' });
                $("#transfer").css({ display:'none' });
                $("#rollback").css({ display:'none' });
            }
        });

        //预计交期
        $("input[name = expected_delivery]").datetimepicker({
            language: 'zh-CN'
            ,format: 'yyyy-mm-dd'
            ,minView: 'month'
            ,todayHighlight: true
            ,autoclose: true
            ,todayBtn: true
        });

        //测试报告上传
        upload.render({
            elem: '#TestReport' //绑定元素
            ,url: '/upload/' //上传接口
            ,accept: 'file' //允许上传的类型
            ,multiple: true  //允许多文件上传
            ,done: function(res){
                //上传完毕回调
                if(res.code === 0){
                    //do something （比如将res返回的图片链接保存到表单的隐藏域）
                }
            }
            ,error: function(){
                //请求异常回调
                layer.msg('上传失败',{icon:2,time:1000});
            }
        });

        //监听提交数据
        form.on('submit(submit)',function (data) {
            if (data.field.type === 'push' && data.field.operator) {
                layer.msg("请选择处理人！");
                return false;
            }
            if (data.field.type === 'transfer' && data.field.transfer) {
                layer.msg("请选择处理人！");
                return false;
            }
            if (data.field.type === 'rollback' && data.field.rollback) {
                layer.msg("请选择处理人！");
                return false;
            }
            
            $.post('', data.field, function (response) {
                if (response.code === 0) {
                    //重新加载页面
                    location.reload();
                } else {
                    layer.msg(response.msg,{icon: 2});
                }
            }, 'json');
        });

        //取消按钮关闭弹窗
        $(".cancel").click(function(){
            layer.closeAll();
        });
    });
});